<?php

namespace App\Http\Controllers\Admin;

use App\Models\Usermemberlevel as memberlevel;
/**
 * Description of UsermemberLevel
 *
 * @author yonguo
 */
class UsermemberlevelController extends BaseController{
    
    /**
     * 用户等级列表
     */
    public function userlevelList()
    {
        if ($this->isOnSubmit()) {
            $levellist = $this->request->input('levelist');
            //dd($levellist);
            if ($levellist) {
                foreach ($levellist as $levelid=>$level) {
                    //$level['level_name'] = intval($level['level_name']);
                    //$level['quota'] = intval($level['quota']);
                    //$level['goods_discount'] = intval($level['goods_discount']);
                    memberlevel::where('level_id', $levelid)->update($level);
                    
                }
                //memberlevel::updateCache();
            }
             return $this->showSuccess('sysmessage.info save success');
        }else {

            $this->assign(['levellist'=>$this->getLevels()]);

            return $this->view('admin.memberlevel.levelist');
        }
        
    }
    
    /**
     * 添加等级
     * @return type
     */
    public function addmemberlevel(){

        $level_id = $this->request->input('level_id');
        if ($this->isOnSubmit()){
            $level = $this->request->post('memberlevel');
            //dd($catlog);
           // return;
            if ($level_id) {
                memberlevel::where('level_id', $level_id)->update($level);
            }else {
                memberlevel::insert($level);
            }
            //Treegame::updateCache();
            return $this->showSuccess('sysmessage.info save success');
        }else {
            $this->assign([
                'level_id'=>$level_id,
                'level'=>[
                    'level_name'=>'',
                    'min_integral'=>'',
                    'quota'=>0,
                    'goods_discount'=>1,
                    'desc'=>'',
                    'is_default'=>'',
                    'upgrade'=>'',
                    'relation'=>''
                ],
                '$levelist'=>$this->getLevels()
            ]);

            if ($level_id) {
                $level = memberlevel::find($level_id);
                if ($level) $this->assign(['level'=>$level]);
            }

            return $this->view('admin.memberlevel.addmemberlevel');
        }
    }
    
    
    /**
     * 删除等级
     * @return type
     */
    public function delete(){
        $level_id = $this->request->input('level_id');
       
           memberlevel::where('level_id', $level_id)->delete();
           
            //Treegame::updateCache();
           
           return $this->showSuccess('sysmessage.info delete success');
           
    }
    
   public function showSuccess($message)
    {
        return $this->messager()->message(trans($message))->render();
        
    }
    
    /**
     * @return array|mixed
     */
    private function getLevels()
    {
        //$levellist = [];
        $levellist = memberlevel::get();
        return $levellist;
    }
}
